# -*- coding:utf-8 -*-
import subprocess, os

class YoutubeUpload(object):
    def __init__(self):
        '''
        上传文件到youtube上
        '''


    def upload(self, video_save_dir = '', title = '', desc = '', json_file = ''):
        '''
        上传视频
        :param video_save_dir:
        :param title:
        :param desc:
        :return: 0 失败 1成功
        '''

        comm = "youtube-upload"
        if video_save_dir == '':
            print('error: 上传视频路径不能为空')
            return 0
        if json_file == '':
            print('error: youtube 授权文件json不能为空')
            return 0

        comm += " " + video_save_dir
        comm += " --title='" + title + "'"
        comm += " --description='" + desc +"'"
        # json 文件是否存在
        if os.path.isfile(json_file) == False:
            print('error: youtube-upload json config file is not exist!!!')

        comm += " --client-secret=" + json_file
        print('\r\n %s \r\n' % comm)
        try:
            status = subprocess.call(comm, shell=True)
        except:
            status = 1

        if status == 0:
            return 1
        else:
            return 0


if __name__ == '__main__':
    test = YoutubeUpload()
    video_dir_save = '/home/youtube/DQ_project/tmp_video_download/6557994514840030478'
    json_file_dir = '/Users/python/youtube/downloadDouUpdateYou/config/client_secret.json'
    video_title = '用户测试'
    video_desc = '描述测试'
    success = test.upload(video_dir_save, video_title, video_desc)
    print('youtube 上传 返回值 1:ok, 0:error  get: %d' % success)
